package com.xiaoxin.nmzp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.xiaoxin.nmzp.server.entity.domain.NmzpCollect;
import com.xiaoxin.nmzp.server.entity.domain.NmzpJob;
import com.xiaoxin.nmzp.server.mapper.NmzpJobMapper;
import com.xiaoxin.nmzp.server.service.NmzpCollectService;
import com.xiaoxin.nmzp.server.mapper.NmzpCollectMapper;
import com.xiaoxin.nmzp.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxin
 * @description 针对表【nmzp_collect】的数据库操作Service实现
 * @createDate 2024-11-05 09:53:05
 */
@Service
public class NmzpCollectServiceImpl extends ServiceImpl<NmzpCollectMapper, NmzpCollect>
        implements NmzpCollectService {
    @Autowired
    private NmzpJobMapper nmzpJobMapper;
    /**
     * 新增职位收藏
     *
     * @param jobId
     */
    @Override
    public void addCollect(Long jobId) {
        NmzpCollect nmzpCollect = new NmzpCollect();
        nmzpCollect.setJobId(jobId);
        nmzpCollect.setUserId(UserUtils.get().getUserId());
        LambdaQueryWrapper<NmzpCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NmzpCollect::getJobId, jobId);
        queryWrapper.eq(NmzpCollect::getUserId, UserUtils.get().getUserId());
        NmzpCollect nc = getOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(nc)){
           this.removeById(nc.getId());
           return;
        }
        this.save(nmzpCollect);
    }

    /**
     * 查询当前用户收藏
     *
     * @return
     */
    @Override
    public List<NmzpJob> getUserCollect() {
        LambdaQueryWrapper<NmzpCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NmzpCollect::getUserId, UserUtils.get().getUserId());
        List<NmzpCollect> list = list(queryWrapper);
        List<NmzpJob> nmzpJobs = new ArrayList<>();
        list.forEach(item->{
            LambdaQueryWrapper<NmzpJob> jobLambdaQueryWrapper = new LambdaQueryWrapper<>();
            jobLambdaQueryWrapper.eq(NmzpJob::getJobId, item.getJobId());
            nmzpJobs.add(nmzpJobMapper.selectOne(jobLambdaQueryWrapper));
        });
        return nmzpJobs;
    }
}




